package com.lmq.chapter01;

public class demo01 {
    public static void main(String[] args) {
        char c = 'x';
        /*
          Autoboxing automatically converts a primitive to a wrapped object:

         */
        Character ch = new Character('x');

        char exc = ch;
        System.out.println(c + " " + ch + " " + exc);
        /**
         * The reasons for wrapping primitives are shown in a later chapter
         */

        // High-precision Numbers
        /**
         *
         * Java includes 2 classes for performing hig-precision arithmetic
         * BigInteger and BigDecimal.
         * These fit approximately the same category  as the "wrapper"
         * classes, neither one has a corresponding primitive.
         * Both classes have methods that provide analogues for the operations
         * you perform on primitive types.
         *
         * You can do anything with a BigInteger or BigDecimal you can with
         * an int or float, it is just that you you must
         * use method calls instead of operators.
         * Also, because there are more calculations involved,
         * the operations are slower.
         * Youre exchangeing speed for accuracy.
         * bigInteger supports arbitrary-precision integers.
         * This means you can accurately represent intgral values of
         * any size without losing any information during operations.
         *
         * BigDecimal is for arbitrary-precision fixed-point numbers, you can use
         * it for accurate monetary calculations,e.g.
         *
         * refer to JDK documentation for details.
         *
         *
         */


        //Array in java
        /**
         * many programming languages support some kind of array.
         * Using arrs in c or c++ is perilious(adj.	危险的; 艰险的)
         * because they are only blocks of memory.If a program access
         * the array outside of its memory block or use the mmory before init
         * the reuslts are unpredctbl
         * One of the primry goal of java is safe, so many problms that
         * plague prgrmers in c and c++ are not repeated in java.
         * A java arr is guaranteed to be init and can not be accesed outside of its range.
         * THis range checking coms at the price of having a small amount of memory overhead
         * for each arr as wellas extra time to veryfy the index at run time
         * but the assption is that the safety and incred productivity are worth
         * the expenese (java cna optmize these)
         * Once create an arr  obj, you are really creating an arry of references,
         * and eahc of these ref is automatically inti to a special value with its
         * own keyword null, When java sees null, it recgnize that the ref in questin is not
         * pint to an object,You must assign an obj to each ref before use it.
         * And if you try to use a ref that's stl null, the problem is reported at run tim
         * Typical arr error are prevent in java
         * You can also create an arry of primitvies.
         * The compiler guart init by zeroing memeory for arr.
         * arry are covered in detail later in book.
         *
         */
        //Comment
        /**
         * 2 type of comment.
         * FIrst is C-style comments which begin with a /*
         * and continue, possible across manyy lines, until a \* /
         * note many progers begin each line of a multiline commnet with
         * a * so you will see
         * Remember, however everything inside the ** is ignored,
         * there is no diff if you say
         *
         * The second form comes from c++. It is the
         * single-line comment, which starts with a // and
         * until end of the line.
         * This type is convinient and commonly used bec it easy
         *
         *
         */

        // You never need to destroy an obj
        /**
         * Managing strage lifetime requires sig effort.
         * How long a var last? if you are spted to destroy it,
         * when should you? Confusion over storge lifetiem can
         * lead to many bug, this section shows how java
         * simplifies the issue by releasing storage for you
         */
        /**
         * Scoping
         *
         * Most procedural lang have the conpt of scop.
         * this determines both the visibility and lftiem
         * of the name defined whithn that scope.
         * In c ++, and java,
         * the scope is determined by the placement of curly braces{}
         * Here is an framgement of java code
         * demonstrating scope:
         * {{
         *     {
         *     int x = 12;
         *     // only x available
         *     {
         *      int q = 96
         *      Both x and q available
         *     }
         *     only x available
         *     q is out of scope
         *
         *     }
         * }}
         *
         * A var defined whithin a scope is available only until the end of
         * that scope.
         * Indention makes java code easier to read
         * Because java is freeform lang, the extr spaces,tab,and carriage
         * returns dont affect the resulting prog.
         * You cant do the following evn it is legla in c and c++
         * {{
         *     int x = 12;
         *     {
         *         int x = 96;// Illegal
         *     }
         * }}
         * JAVA compiler will annce that variable x has already been defined,
         * Thus c and c++ ability to hide a variable in a lager scope is not
         * allowd bec java designers though itlead ot confuseing program.
         */
        //        int x = 12;
        //        {
        //            int x = 96; // Variable 'x' is already defined in the scope
        //        }
        /**
         * scop of obj
         * java obj dont hve same liftm as prmitvs.
         * When you create java obj using new, it persisits past the end of the scop.
         * THus if you say
         * String s= new String("a string");
         * the ref s vanishes at the end of the scope.However, THe string
         * obj that s points to is still ocupying mery.In this bit of code,
         * there is no way to access the obj after the end of scope, because the only ref to
         * it is out of scope.
         * In later chp, you will see how the ref to obj can be passed around and
         * duplicated using the course of a prog.
         */

        // Creating new data type class
        /**
         * IF evthing is an obj, what determines how a paritcular class of obj looks and behaves?
         * put another way , what establishes the type of an obj?
         * You might expect a keyword called type,
         * and that would certainly make sense,
         * Historically, however, most oo lang use keyword class to describe a new kind of onbj.
         * the class keyword so comon it will often not be hold faced throughout
         * this book is followed by the name of the new type.
         */
        class ATypeName{
            // class body goes here.
        }
        ATypeName a = new ATypeName();
        //Fields
        /**
         * WHen you define a class, you can put 2 types of elements in your class:
         * fields ( sometime called data members ) and methods also called member functions)
         * A fields is an obj of an type you can talk to via its ref.
         * It can also be a prim type.
         * If it i s a ref to an obj, you must ini that ref to
         * connet it to an actual obj(using new as seen earlier)>
         * Each obj keep itw own storage for its fields. Ordinarily,
         * fields are not shared among obj. Here is an example of a class with some fields.
         *
         */
        class DataOnly{
            int i;
            double d;
            boolean b;
        }
        /**
         * This class dont do anything exp hold data. As before, you create an obj
         * like this
         * DataOnly data = new DataOnly();
         */
        DataOnly data = new DataOnly();
        // Assign values to the fields by referring to obj members .
        // State the name of the obj ref , followed by a period dot, followed by the
        // name of the member inside the obj: objRef.member
        data.i = 47;
        data.d = 1.1;
        data.b = false;
        /**
         * What if your obj contains otehr objs that contain data you wnt to modify?
         * You just keep connnecting the dots . for example:
         * myPlane.leftTank.capacity = 100;
         * nest many obj this way , although such a desingn might becmome confusing
         *
         */

        // Default values for premitive members
        /**
         * When priv data is a field in class, it is gurteed to
         * get a default value if you dont inti.
         * Deflt value only what java guarantees when the var is used as a member of a class
         * This ensures that prmit fields will alwys be init (something c++ dosent do),
         * reducing a source of bugs. THis inti value might not be correct or
         * even legal for you prog you are writing. Its best to alwys
         * explicitly initialize your varibles.
         * THis gurantee dosent apllly to your local variables
         * THose taht not fields of a class
         * if within a method def you have
         * int x;
         * x will get some arbitrary value(as it doese in c and c++_
         * it willnot automatically be init to zero.
         * You are repsble for assingnig an appropriate value before you use
         * x.
         * If you forget, java definitely improves on c++:you get
         * a compile time error telling you var might not be initialized.
         * C++ COMPLIERS OFTEN warn you about uniit var, but java these are errors.
         */

        //Methods arguments and return values
        /**
         * c and c++ , the funciton is used to desc
         * a nem subroutine.
         * java we use term method as in a way  to do sth.
         * Mthd in java determine the messages an obj can receive.
         * The fundamental parts of amethod are the name, the arguemnts, the
         * return type and the body.
         * Here is hte basic form:
         * ReturnType methodName(/ Arugument list//){
         * //Method body
         * }
         *
         * ReturnType indicates the type of value produce by the methd when
         * you call it.Arg list givbes the types and name for the info you pass inot
         * the mehod.THe method name and arg list are collectively called signature of
         * the method. The signature uniquely identifies that method.
         * Mth in java cna only be create as part of a class .
         * A method can be called only for an obj. that obj must be able to perfm that method call.
         * IF you try ot cll wrong method for obj, get an error messg at complie time.
         * cll methd for obj by giv obj ref folled by a perid dot , flled by the name of the methd and it arg lsit
         * like objRef.methdNme(arg1,arg2,arg3)
         * CONSIDE methd f() that tak no arg and ret a vl type int,For
         * a ref a that accept calls to f(),
         * you can sy : int x = a.f();
         * the type return val must compatible with tyhpe of x;
         * this act of cll methd is smtimes termed sending a mess to an obj.
         * In preceding exmple, mess is f() and obj is a.oo p can eb sumrized as sending mess to obj.
         *
         *
         *
         */
        // Arg list
        /**
         * methd arg lsit specifies the info you pss to methd.
         * As you mgth guess, this info lke everything else in java tak the fom
         * of obj.THe arg list must spfy obj type and the name of each obj.
         * As always, where you seem to handing obj aroud, you actly passing ref.
         *
         */


        /**
         * Both st1.i and st2.i have the same value of 47 because they are the same piece of
         * memory.
         * You can also refer to it directly through
         * its class name,
         * something you cannot do with a non-static member:
         */
        StaticTest st1 = new StaticTest();
        StaticTest st2 = new StaticTest();
        System.out.println(st1.i);
        System.out.println(StaticTest.i++);
        System.out.println(StaticTest.i);
        Incrementable sf = new Incrementable();
        sf.increment();

        Incrementable.increment();










    }

    int storage(String s ){

        return s.length() * 2;
    }
}

class StaticTest {
    static  int i = 47;
}

/**
 * simlar logic aplies to static methods.
 * Ref to a static method either through an obj
 * as you can with mth, or with special adtion sytax
 * clasNmae.method. You can define static method like
 */
class Incrementable {
    static void increment() { StaticTest.i++; }


}